package demo4.controller;


import demo4.mapper.iUserMapper;
import demo4.pojo.User;
import demo4.util.DBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

/**
 * Controller 控制器
 * 该层级的类主要的工作就是与用户打交道
 * 接受用户的请求，后期协调业务类完成业务处理，并将结果回馈给用户
 * <p>
 * UserController
 * 负责处理与"用户"数据相关的请求，比如注册，登录，修改密码等等
 */
@Component
public class UserController {
    @Autowired
    private iUserMapper userMapper;

    public void start() {
        System.out.println("请输入操作序号:");
        Scanner scanner = new Scanner(System.in);
        System.out.println("1:注册");
        System.out.println("2:登录");
        System.out.println("3:修改密码");
        System.out.println("4:用户列表");
        int num = scanner.nextInt();//获取用户输入的序号
        switch (num) {
            case 1:
                regUser();
                break;
            case 2:
                loginUser();
                break;
            case 3:
                updateUser();
                break;
            case 4:
                listUser();
                break;
            default:
                System.out.println("输入错误");
        }
    }


    /**
     * 处理用户修改密码
     */
    private void updateUser() {
        System.out.println("修改密码");
        /*

         */
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输旧密码");
        String oldPassword = scanner.nextLine();
        System.out.println("请输入新密码");
        String newPassword = scanner.nextLine();
        User user = userMapper.selectUserByUsername(username);
        if (user != null) {
            if (user.getPassword().equals(oldPassword)) {
                user.setPassword(newPassword);
                int num = userMapper.updateUser(user);
                System.out.println(num > 0 ? "修改成功" : "修改失败");

            } else {
                System.out.println("旧密码错误");
            }
        }else {
            System.out.println("用户不存在");}
    }


        /**
         * 处理用户列表
         */
        private void listUser () {
            List<User> userlist = userMapper.selectAllUser();
            for (User user : userlist) {
                System.out.println(user);
            }

        }


        /**
         * 处理用户登录
         */
        private void loginUser () {
         /*
            1:获取用户输入的用户名和密码
            2:调用持久层功能，将用户数据查询出来
            3:判断登录(用户名和密码都输入正确，才算登录成功)
            4:根据判断结果提示用户登录是否成功
         */
            System.out.println("欢迎登录");
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名:");
            String username = scanner.nextLine();
            System.out.println("请输入密码：");
            String password = scanner.nextLine();
            User user = userMapper.selectUserByUsername(username);
            if (user != null) {
                if (user.getPassword().equals(password)) {
                    ;
                    System.out.println("登录成功，欢迎【" + user.getNickname() + "】回来");
                } else {
                    System.out.println("密码不正确");
                }
            } else {
                System.out.println("用户不存在");

            }


        }

        /**
         * 处理用户注册
         */
        private void regUser () {
            System.out.println("欢迎注册");
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名");
            String username = scanner.nextLine();
            System.out.println("请输入密码");
            String password = scanner.nextLine();
            System.out.println("请输入昵称");
            String nickname = scanner.nextLine();
            System.out.println("请输入年龄");
            int age = scanner.nextInt();
            //必要的验证
            //是否没输入信息，用户名是否重复...
            if (userMapper.selectUserByUsername(username) != null) {
                System.out.println("用户名已存在");
                return;
            }

            //用一个User对象表示注册信息
            User user = new User(0, username, password, nickname, age);


            //调用持久层的功能，将用户数据进行保存
            int result = userMapper.insertUser(user);
            //返回结果给用户
            System.out.println(result > 0 ? "注册成功" : "注册失败");
        }
    }










